package com.zl.init;

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.zl.framework.util.PropConfig;

public class SSHDBUtil {
    private static PropConfig propConfig = PropConfig.loadConfig("setting-web.properties");
    ;
    private static String filePath = "D://vip_rsa";

    public static void initDB(String host, int rport, int lport)
    {
        String env = propConfig.getConfigString("runtime.env");
        System.out.println(String.format("host:%s,rport:%s,lport:%s,env:%s", host,rport,lport,env));
        Session session = null;
        try {
            JSch jsch = new JSch();
            if (("vip".equals(env) || "prod".equals(env)) && rport != 2181) {
                jsch.addIdentity(filePath);
                session = jsch.getSession("root", "172.19.233.159", 22);
            } else {
                session = jsch.getSession("root", "192.168.2.2", 22);
                session.setPassword("Zhonglun@2019");
            }
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            config.put("userauth.gssapi-with-mic", "no");
            //config.put("Compression", "yes");
            //config.put("ConnectionAttempts", "2");
            session.setConfig(config);
            // Connect
            System.out.println("开始连接");
            session.connect();
            System.out.println("连接成功,开始转发");
            session.setPortForwardingL(lport, host, rport);
            System.out.println("转发成功");
        } catch (Exception e) {
            System.out.println("连接异常");
            e.printStackTrace();
            session.disconnect();
        }
    }

}
